package com.k2.whereareyou;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.k2.whereareyou.engine.Engine;
import com.k2.whereareyou.interfaces.IConstants;
import com.k2.whereareyou.web.RequestController;
import com.k2.whereareyou.xml.PassXML;

public class RecoverPassword extends Activity implements IConstants, View.OnClickListener{
	
	private Button[] buttons;
	private TextView[] textViews;
	private EditText[] editTexts;
	private byte language;
	private PassXML pass;
	private String message = "";

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        language = Engine.setLanguage(getApplicationContext());
        initLayout();
		initUI();
    }
	
	
	private void initUI(){
        
    	buttons = new Button[BUTTONS_NUMBER_RECOVER_PHOTO.length];
    	Log.d("add_buttons", String.valueOf(buttons.length));
    	for (int i=0; i < BUTTONS_NUMBER_RECOVER_PHOTO.length; i++){
    		buttons[i] = (Button) findViewById(BUTTONS_NUMBER_RECOVER_PHOTO[i]);
    		buttons[i].setText(TEXT_RECOVER_PHOTO[language][2+i]);
    		buttons[i].setOnClickListener(this);
    	}
    	
    	textViews = new TextView[TEXTVIEWS_NUMBER_RECOVER_PHOTO.length];
    	for (int i=0; i < TEXTVIEWS_NUMBER_RECOVER_PHOTO.length; i++){
    		textViews[i] = (TextView) findViewById(TEXTVIEWS_NUMBER_RECOVER_PHOTO[i]);
    		textViews[i].setText(TEXT_RECOVER_PHOTO[language][i]);
    	}
    	
    	editTexts = new EditText[EDITTEXTS_NUMBER_RECOVER_PHOTO.length];
    	for (int i=0; i < EDITTEXTS_NUMBER_RECOVER_PHOTO.length; i++){
    		editTexts[i] = (EditText) findViewById(EDITTEXTS_NUMBER_RECOVER_PHOTO[i]);
    	}
    	
	}
	
	private void initLayout() {
		setContentView(R.layout.recover_password);
	}
	
	@Override
	public void onClick(View activity) {
		switch (activity.getId()) {
	    case R.id.button_recover_pass:
	    	createPassXML();
	      break;
	    default:
	      break;
	    }
	}
	
	private void createPassXML() {
		message = Engine.validation(editTexts[0].getText().toString(), "1", language);
		if(message.length()!=0){
			Context context = getApplicationContext();
	        Toast toast = Toast.makeText(context, 
	        		message, Toast.LENGTH_SHORT);
	        toast.setGravity(Gravity.CENTER, 0, 0);
	        toast.show();

		}
		else {
			pass = new PassXML();
			pass.setEmail(editTexts[0].getText().toString());
			String ansver = RequestController.getResponseByXml("url", pass);
	    	if (ansver == null){
	    		
	    	} else {
	    		startActivity(new Intent(this, MainActivity.class));
	    		System.exit(0);
	    	}
		}
	}


	@Override
	public void onBackPressed() {
		startActivity(new Intent(this, MainActivity.class));
		System.exit(0);
	}

}
